package wechat;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PushMessageServiceImpl {

    @Autowired
    private WxMpService wxMpService;

    public void sendMessage(String openId,String templatedId) {
        WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
        wxMpTemplateMessage.setToUser(openId);                                   //接收消息用户的openid,是要这个应用的openid
        wxMpTemplateMessage.setTemplateId(templatedId);  //具体用哪个消息模板,我这里把模板id配置到了yml,大家可以自己写死或者按照配置传

        List<WxMpTemplateData> data = new ArrayList<WxMpTemplateData>(){{
            add(new WxMpTemplateData("first","亲记得收货"));
            add(new WxMpTemplateData("keyword1","微信点餐"));
            add(new WxMpTemplateData("keyword2","131311"));
            add(new WxMpTemplateData("keyword3",""));
            add(new WxMpTemplateData("keyword4",""));
            add(new WxMpTemplateData("keyword5","¥" + ""));
            add(new WxMpTemplateData("remark", "假装有个备注"));
        }};
        wxMpTemplateMessage.setData(data);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
